export const baseMenu = "/Admin/";

// 所有菜单数据：
export const allMenus = [
    {
        id: "1",
        label: "CRUD",
        path: "CRUD",
        name: "CRUD",
        component: () => import("../views/CRUD/index.vue"),
        
    },
    {
        id: "2",
        label: "pinia示例",
        name: "piniademo",
        path: "piniademo",
        component: () => import("../views/piniaDemo/index.vue")
    },
    {
        id: "3",
        label: "操作人员",
        name: "Operator",
        path: "Operator",
        component: () => import("../views/Operator/index.vue")
    },
    {
        id: "4",
        label: "生命周期",
        children: [
            {
                id: "4-1",
                label: "选项式的",
                name: "Lifecycle",
                path: "Lifecycle_options",
                component: () => import("../views/Lifecycle/Lifecycle_options.vue"),

            },
            {
                id: "4-2",
                label: "组合式的",
                name: "Lifecycle_composition",
                path: "Lifecycle_composition",
                component: () => import("../views/Lifecycle/Lifecycle_composition.vue")
            },
            {
                id: "4-3",
                label: "父子组件钩子调用顺序",
                name: "Lifecycle_ParentSon",
                path: "Lifecycle_ParentSon",
                component: () => import("../views/Lifecycle/Lifecycle_ParentSon/Parent.vue"),
            }
        ]
    },
    {
        id: "5",
        label: "组件传值",
        children: [
            {
                id: "5-1",
                label: "父子组件传值",
                name: "ParentToSon",
                path: "ParentToSon",
                component: () => import("../views/ComponentChuan/ParentToSon/Parent.vue")
            },
            {
                id: "5-2",
                label: "父子组件传值（事件总线）",
                name: "Brother",
                path: "Brother",
                component: () => import("../views/ComponentChuan/Brother/index.vue")

            },
            {
                id: "5-3",
                label: "事件总线案例",
                name: "TransferValueDemo",
                path: "TransferValueDemo",
                component: () => import("../views/ComponentChuan/demo/index.vue")

            },
            {
                id: "5-4",
                label: "父子案例",
                name: "TransferValueDemo02",
                path: "TransferValueDemo02",
                component: () => import("../views/ComponentChuan/demo02/index.vue")

            },
            {
                id: "5-5",
                label: "ref(选项式)",
                name: "refdemo",
                path: "refdemo",
                component: () => import("../views/ComponentChuan/refdemo/Parent.vue")
            },
            {
                id: "5-6",
                label: "ref(组合式)",
                name: "refdemo02",
                path: "refdemo02",
                component: () => import("../views/ComponentChuan/refdemo02/Parent.vue")
            },
            {
                id: "5-7",
                label: "跨级组件传值",
                name: "providerAndInject",
                path: "providerAndInject",
                component: () => import("../views/ComponentChuan/providerAndInject/Parent.vue")
            }
        ]
    },
    {
        id: "6",
        label: "路由传参",
        children: [
            {
                id: "6-1",
                label: "路由传参（params）",
                name: "List",
                path: "List",
                component: () => import("../views/routeChuan/params/List.vue")
            },
            {
                id: "6-2",
                label: "路由传参（query）",
                name: "List02",
                path: "List02",
                component: () => import("../views/routeChuan/query/List.vue")
            },
            {
                id:"6-3",
                name: "Detail",
                path: "Detail/:id",
                component: () => import("../views/routeChuan/params/Detail.vue"),
                isMenu:false
            },
            {
                id:"6-4",
                name: "Detail02",
                path: "Detail02",
                component: () => import("../views/routeChuan/query/Detail.vue"),
                isMenu:false
            },
        ]
    },
    {
        id: "7",
        label: "vuex",
        children: [
            {
                id: "7-1",
                label: "vuex示例(基本使用)",
                name: "vuexdemo01",
                path: "vuexdemo01",
                component: () => import("../views/vuexdemo/index.vue")
            },
            {
                id: "7-2",
                label: "vuex示例（模块化）",
                name: "vuexdemo02",
                path: "vuexdemo02",
                component: () => import("../views/vuexdemo/goods.vue")
            }
        ]
    }
]


// 根据后端响应回来的checkedKeys，从allMenus里过滤出当前用户的菜单

export const getCurrentMenus = () => {

    // 1、获取当前用户的checkedKeys。
    // ["1","4-2","4-3"]
    const checkedKeys = JSON.parse(sessionStorage.getItem("checkedKeys"));


    const resultMenus = [];

    // 2、过滤：循环allMenus，判断当前菜单项的id是否在checkedKeys
    allMenus.forEach(item => {
        if (!item.children) {
            if (checkedKeys.includes(item.id) && item.isMenu !=false) {
                resultMenus.push({ ...item });
            }
        } else {
            // 有子级
            // 1)、遍历子级，
            // 看看子级是否有id在checkedKeys中。如果有，push父级，如果没有，不push父级
            if (item.children.some(sonItem => (checkedKeys.includes(sonItem.id) && sonItem.isMenu !=false))) {
                // 父级push了
                const parentMenu = { ...item };
                resultMenus.push(parentMenu);
                // 处理子级
                parentMenu.children = parentMenu.children.filter(item => (checkedKeys.includes(item.id) && item.isMenu !=false));
            }
        }
    })
    
    resultMenus.push({
        id: "3",
        label: "操作人员",
        name: "Operator",
        path: "Operator",
        component: () => import("../views/Operator/index.vue")
    },);

    console.log("resultMenus", resultMenus);
    return resultMenus;
}
